<?php
//header("Content-type:text/html;charset=utf-8");
require_once("request.php");
require_once("addUserInfo.php");

$code=$_GET["code"];
$state=$_GET["state"];

$response =getTokenInfo($code);
$obj = json_decode($response);
$access_token =$obj->access_token;
$openid =$obj->openid;


//获取用户信息
$userinfo=getUserInfo($access_token,$openid);
$userObj =json_decode($userinfo);
$nickname=$userObj->nickname;
$sex=$userObj->sex;
$headimgurl=$userObj->headimgurl;


//开启这个数组
@session_start();
//储存数据
//$_SESSION['openid']=$openid;


$_SESSION['nickname']=$nickname;
$_SESSION['city']=$city;
$_SESSION['headimgurl']=$headimgurl;


//把用户添加倒数据库中
 addUserInfo($userObj);

echo "<script>window.location.href='http://1.happyer.applinzi.com/phone_two.html'</script>";


//获取access_token 和openid
function getTokenInfo($code){
  $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx9bdc780b9673cf2e&secret=f5a8570a60a826631e255178fa729411&code={$code}&grant_type=authorization_code";
  return httpGet($url);
}

//获取用户信息
function getUserInfo($access_token,$openid){
  $url="https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&sex={$sex}&lang=zh_CN ";
  return httpGet($url);
}

 ?>
